from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.prop_fields_def import prod_tag_fields,prod_tag_list_fields,prod_tag_ref_list_fields,prod_tag_ref_fields
from services.online_mall.prod_tag_service import ProdTagService


class ProdTagsApi(Resource):
    @login_required
    @marshal_with(prod_tag_list_fields)
    @check_perms(perms=['prod:tag:list'])
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("title",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        parser.add_argument("is_system",type=StrBoolNone,required=False,location='args')
        parser.add_argument("list_style",type=int,required=False,location='args')
        parser.add_argument("page",type=int,required=False,location='args')
        parser.add_argument("page_size",type=int,required=False,location='args')
        args = parser.parse_args()
        return ProdTagService.get_tag_list(current_user.current_tenant_id,args)
    
    @login_required
    @marshal_with(prod_tag_fields)
    @check_perms(perms=['prod:tag:create'])
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument("title",type=str,required=True,location='json')
        parser.add_argument("shop_id",type=str,required=False,location='json')
        parser.add_argument("status",type=StrBoolNone,required=False,location='json')
        parser.add_argument("is_system",type=StrBoolNone,required=False,location='json')
        parser.add_argument("list_style",type=int,required=False,location='json')
        parser.add_argument("prod_count",type=int,required=False,location='json')
        parser.add_argument("seq",type=int,required=False,location='json')
        args = parser.parse_args()
        return ProdTagService.create_tag(current_user,args)
    
    @login_required
    @marshal_with(prod_tag_fields)
    @check_perms(perms=['prod:tag:update'])
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        parser.add_argument("title",type=str,required=True,location='json')
        parser.add_argument("shop_id",type=str,required=False,location='json')
        parser.add_argument("status",type=StrBoolNone,required=False,location='json')
        parser.add_argument("is_system",type=StrBoolNone,required=False,location='json')
        parser.add_argument("list_style",type=int,required=False,location='json')
        parser.add_argument("prod_count",type=int,required=False,location='json')
        parser.add_argument("seq",type=int,required=False,location='json')
        args = parser.parse_args()
        return ProdTagService.update_tag(current_user,args)
    
    @login_required
    @marshal_with(prod_tag_fields)
    @check_perms(perms=['prod:tag:del'])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        args = parser.parse_args()
        tag_id = args.get("id")
        return ProdTagService.delete_tag(current_user,tag_id)
    
class ProdTagReferencesApi(Resource):
    @login_required
    @marshal_with(prod_tag_ref_list_fields)
    @check_perms(perms=['prod:tag_ref:list'])
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("tag_id",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        parser.add_argument("page",type=int,required=False,location='args')
        parser.add_argument("page_size",type=int,required=False,location='args')
        args = parser.parse_args()
        return ProdTagService.get_tag_ref_list(current_user.current_tenant_id,args)
    
    @login_required
    @marshal_with(prod_tag_ref_fields)
    @check_perms(perms=['prod:tag_ref:create'])
    def post(self):
        parser = reqparse.RequestParser()
        # parser.add_argument("id",type=str,required=False,location='args')
        parser.add_argument("tag_id",type=str,required=True,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("prod_id",type=str,required=True,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        args = parser.parse_args()
        return ProdTagService.create_tag_ref(current_user,args)
    
    @login_required
    @marshal_with(prod_tag_ref_fields)
    @check_perms(perms=['prod:tag_ref:update'])
    def put(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=int,required=True,location='args')
        parser.add_argument("tag_id",type=str,required=True,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("prod_id",type=str,required=True,location='args')
        parser.add_argument("status",type=StrBoolNone,required=False,location='args')
        args = parser.parse_args()
        return ProdTagService.update_tag_ref(current_user,args)

    @login_required
    @check_perms(perms=['prod:tag_ref:del'])
    def delete(self):
        parser = reqparse.RequestParser()
        parser.add_argument("id",type=int,required=True,location='args')
        args = parser.parse_args()
        ref_id = args["id"]
        return ProdTagService.del_tag_ref(current_user,ref_id)
    
api.add_resource(ProdTagReferencesApi,'/prod/tags/ref')
api.add_resource(ProdTagsApi,'/prod/tags')